package com.jackrain.nea.fc.dl.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Mapper
public interface ViewBatchUpdateMaper {

    //-------拣货--------
    @Select("SELECT BILL_NO" +
            " FROM dl_b_tran a " +
            "WHERE A.TRAN_B_BILL_BOX_ID = #{id} AND A.IS_PICK = 'N' AND A.ISACTIVE = 'Y'")
    List<HashMap> selBillNo(@Param("id") Long id);

    @Select("SELECT BILL_NO,CP_C_ORIG_ID,CP_C_DEST_ID,BILL_DATE,SEND_TIME,CYCLE_DAYS,LOGISTICS,VARCHAR5,VARCHAR2,SUM_QTY_OUT FROM DL_B_TRAN WHERE IS_PICK = 'Y' AND SEND_STATUS = 2 AND TRAN_FLAG <> 1 AND ID IN (#{ids})")
    HashMap selBatchUpdate(
            @Param("ids") Long ids);

    @SelectProvider(type = ViewBatchUpdateMaper.ViewBoxSql.class, method = "selectBoxId")
    List<HashMap> selBoxId(@Param("id") Object[] id);

    @SelectProvider(type = ViewBatchUpdateMaper.ViewBoxSql.class, method = "selOrderSign")
    List<HashMap> selOrderSign(@Param("id") Object[] id);

    //-------发出--------
    @Select("SELECT ID,BILL_NO,CP_C_ORIG_ID,CP_C_DEST_ID,SKU_NUMBER,PACKAGE_COUNT FROM DL_B_TRAN_MERGE WHERE  ID IN (#{ids})")
    HashMap selComBatchUpdate(
            @Param("ids") Long ids);

    @SelectProvider(type = ViewBatchUpdateMaper.ViewBoxSql.class, method = "selComOrderSign")
    List<HashMap> selComOrderSign(@Param("id") Object[] id);

    @Select("SELECT A.DL_B_TRAN_BILL_NO FROM DL_B_TRAN_MERGE_ITEM A WHERE A.DL_B_TRAN_MERGE_ID = #{ids}")
    List<HashMap> selItemBillNo(@Param("ids") Long ids);

    @Select("SELECT MAX(AA.SEND_TIME) AS SEND_TIME, "+
            "MAX(AA.CYCLE_DAYS) AS CYCLE_DAYS, "+
            "MAX(AA.BILL_DATE) AS BILL_DATE, "+
            "MAX(AA.LOGISTICS) AS LOGISTICS "+
            "FROM DL_B_TRAN AA "+
            "WHERE AA.ID IN (SELECT DL_B_TRAN_ID FROM DL_B_TRAN_MERGE A,DL_B_TRAN_MERGE_ITEM B WHERE A.ID=B.DL_B_TRAN_MERGE_ID AND A.ID=#{ids} GROUP BY DL_B_TRAN_ID)")
    HashMap selStDaLog(@Param("ids") Long ids);

    @SelectProvider(type = ViewBatchUpdateMaper.ViewBoxSql.class, method = "selMergeId")
    List<HashMap> selMergeId(@Param("id") Object[] id);

    class ViewBoxSql {
        public String selMergeId(Map<String, Object> map) {
            Object[] id = (Object[]) map.get("id");
            return new SQL() {
                {
                    SELECT(" DL_B_TRAN_MERGE_ID ");
                    FROM("DL_B_TRAN_MERGE_ITEM");
                    StringBuffer sb = new StringBuffer();
                    for (int i = 0; i < id.length; i++) {
                        sb.append(id[i] + ",");
                    }
                    sb.deleteCharAt(sb.length() - 1);
                    WHERE(" DL_B_TRAN_ID IN (" + sb.toString() + ") ");
                }
            }.toString();
        }
        public String selOrderSign(Map<String, Object> map) {
            Object[] id = (Object[]) map.get("id");
            return new SQL() {
                {
                    SELECT(" ID , BILL_NO , ARRIVAL_TIME , VARCHAR2 ");
                    FROM("DL_B_TRAN");
                    StringBuffer sb = new StringBuffer();
                    for (int i = 0; i < id.length; i++) {
                        sb.append(id[i] + ",");
                    }
                    sb.deleteCharAt(sb.length() - 1);
                    WHERE("IS_PICK = 'Y' AND SEND_STATUS = 2 AND ACCEPTE_STATUS = 2 and TRAN_FLAG <> 1 AND ID IN (" + sb.toString() + ") ");
                }
            }.toString();
        }
        public String selComOrderSign(Map<String, Object> map) {
            Object[] id = (Object[]) map.get("id");
            return new SQL() {
                {
                    SELECT(" ID , BILL_NO , ACCEPT_TIME ");
                    FROM("DL_B_TRAN_MERGE");
                    StringBuffer sb = new StringBuffer();
                    for (int i = 0; i < id.length; i++) {
                        sb.append(id[i] + ",");
                    }
                    sb.deleteCharAt(sb.length() - 1);
                    WHERE("ID IN (" + sb.toString() + ")");
                }
            }.toString();
        }
        public String selectBoxId(Map<String, Object> map) {
            Object[] id = (Object[]) map.get("id");
            return new SQL() {
                {
                    SELECT(" ID , TRAN_B_BILL_BOX_ID ");
                    FROM("dl_b_tran");
                    StringBuffer sb = new StringBuffer();
                    for (int i = 0; i < id.length; i++) {
                        sb.append(id[i] + ",");
                    }
                    sb.deleteCharAt(sb.length() - 1);
                    WHERE("ID IN (" + sb.toString() + ")");
                }
            }.toString();
        }
    }
}